<?php

// Set this to true if you want to be able to load images from a url that doesn't
// end in an image file extension. E.g. through another proxy of kinds.
define('ALLOW_NO_EXT', false);
//resize width
$width = 0 ;
$height = 0 ;
//crop width
$cWidth = 0 ;
$cHeight = 0 ;
if(isset($_REQUEST['w'])){
    $width = $_REQUEST['w'];
}
if(isset($_REQUEST['h'])){
    $height = $_REQUEST['h'];
}
if(isset($_REQUEST['cw'])){
    $cWidth = $_REQUEST['cw'];
}
if(isset($_REQUEST['ch'])){
    $cHeight = $_REQUEST['ch'];
}
if (!$_GET['url']) {
    exit;
}
// Grab the URL
$url = trim(urldecode($_GET['url']));

$urlinfo = parse_url($url, PHP_URL_PATH);
$ext = array_reverse(explode(".", $urlinfo));

$ctype = null;
switch ($ext[0]) {
    case 'gif': $ctype = 'image/gif';
        break;
    case 'png': $ctype = 'image/png';
        break;
    case 'jpeg':
    case 'jpg': $ctype = 'image/jpg';
        break;
    default:
        if (ALLOW_NO_EXT) {
            $ctype = 'application/octet-stream';
        } else {
            exit;
        }
}
$image = file_get_contents($url);
$tmp_image = tempnam("/tmp", "tmp_image");
file_put_contents($tmp_image, $image);
// Route the image through this script
header("Content-Type: $ctype");
$nImage = new Imagick($tmp_image);
if($width != 0 || $height != 0){
    $nImage->resizeImage($width,$height, imagick::FILTER_LANCZOS, 0.9);
}
if($cWidth != 0 || $cHeight != 0){
     $nImage->cropImage(67, 50, 0, 0);
}
echo $nImage;